package com.game.doudizhu.controller;

import com.game.doudizhu.model.GameRoom;
import com.game.doudizhu.model.Player;
import com.game.doudizhu.model.ShopItem;
import com.game.doudizhu.service.AdminService;
import com.game.doudizhu.service.FileUploadService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/admin")
@RequiredArgsConstructor
public class AdminController {

    private final AdminService adminService;
    private final FileUploadService fileUploadService;

    @GetMapping("/dashboard")
    public ResponseEntity<Map<String, Object>> getDashboardData() {
        return ResponseEntity.ok(adminService.getDashboardData());
    }

    /** users api */
    @GetMapping("/users")
    public ResponseEntity<List<Player>> getAllUsers() {
        return ResponseEntity.ok(adminService.getAllUsers());
    }

    @PostMapping("/users/{userId}/ban")
    public ResponseEntity<Void> banUser(@PathVariable Long userId) {
        adminService.toggleUserBan(userId);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/users/{userId}")
    public ResponseEntity<Void> deleteUser(@PathVariable Long userId) {
        adminService.deleteUser(userId);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/users/search")
    public ResponseEntity<List<Player>> searchUsers(@RequestParam String keyword) {
        return ResponseEntity.ok(adminService.searchUsers(keyword));
    }

    @GetMapping("/users/statistics")
    public ResponseEntity<Map<String, Object>> getUserStatistics() {
        return ResponseEntity.ok(adminService.getUserStatistics());
    }

    @PostMapping("/users/{userId}/reset-password")
    public ResponseEntity<Void> resetUserPassword(
            @PathVariable Long userId,
            @RequestBody Map<String, String> request
    ) {
        adminService.resetUserPassword(userId, request.get("newPassword"));
        return ResponseEntity.ok().build();
    }

    @PostMapping("/users/{userId}/update-score")
    public ResponseEntity<Void> updateUserScore(
            @PathVariable Long userId,
            @RequestBody Map<String, Integer> request
    ) {
        adminService.updateUserScore(userId, request.get("scoreChange"));
        return ResponseEntity.ok().build();
    }
    /** users api end */

    /** users api */
    @GetMapping("/rooms")
    public ResponseEntity<List<GameRoom>> getAllRooms() {
        return ResponseEntity.ok(adminService.getAllRooms());
    }

    @PostMapping("/rooms/{roomId}/close")
    public ResponseEntity<Void> closeRoom(@PathVariable Long roomId) {
        adminService.closeRoom(roomId);
        return ResponseEntity.ok().build();
    }
    /** users api end */


    /** shop api */
    @GetMapping("/shop/items")
    public ResponseEntity<List<ShopItem>> getAllShopItems() {
        return ResponseEntity.ok(adminService.getAllShopItems());
    }

    @GetMapping("/shop/items/{itemId}")
    public ResponseEntity<ShopItem> getShopItem(@PathVariable Long itemId) {
        return ResponseEntity.ok(adminService.getShopItem(itemId));
    }

    @PostMapping("/shop/items")
    public ResponseEntity<ShopItem> createShopItem(@RequestBody ShopItem item) {
        return ResponseEntity.ok(adminService.createShopItem(item));
    }

    @DeleteMapping("/shop/items/{itemId}")
    public ResponseEntity<Void> deleteShopItem(@PathVariable Long itemId) {
        adminService.deleteShopItem(itemId);
        return ResponseEntity.ok().build();
    }

    @PutMapping("/shop/items/{itemId}")
    public ResponseEntity<ShopItem> updateShopItem(
            @PathVariable Long itemId,
            @RequestBody ShopItem item) {
        return ResponseEntity.ok(adminService.updateShopItem(itemId, item));
    }

    @PostMapping("/shop/upload")
    public ResponseEntity<Map<String, String>> uploadImage(@RequestParam("file") MultipartFile file) {
        try {
            String imageUrl = fileUploadService.uploadFile(file);
            Map<String, String> res = new HashMap<>();
            res.put("url", imageUrl);
            return ResponseEntity.ok(res);
        } catch (IOException e) {
            throw new RuntimeException("文件上传失败", e);
        }
    }
    /** shop api end */

    /** settings api */
    @GetMapping("/settings")
    public ResponseEntity<Map<String, Object>> getSystemSettings() {
        return ResponseEntity.ok(adminService.getSystemSettings());
    }

    @PostMapping("/settings")
    public ResponseEntity<Void> updateSystemSettings(@RequestBody Map<String, Object> settings) {
        adminService.updateSystemSettings(settings);
        return ResponseEntity.ok().build();
    }
    /** settings api end */
}